<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4">
<head>
    <title>任务列表</title>
    <div th:replace="common/link::header"></div>
</head>
<body>
<div class="layui-fluid">
    <div class="layui-card">
        <form class="layui-form layui-card-header layuiadmin-card-header-auto">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">任务名字</label>
                    <div class="layui-input-block">
                        <input type="text" name="jobName" placeholder="请输入任务名字" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">任务组</label>
                    <div class="layui-input-block">
                        <input type="text" name="jobGroup" placeholder="请输入任务组" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <button type="button" class="layui-btn layuiadmin-btn-useradmin" lay-submit lay-filter="search">
                        <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                    </button>
                    <button type="reset" class="layui-btn layui-btn-primary" lay-submit lay-filter="reset">重置</button>
                </div>
            </div>
        </form>
        <div class="layui-card-body">
            <table id="jobTable" lay-filter="jobTable"></table>
            <script type="text/html" id="toolbar">
                <div class="layui-btn-container">
                    <a sec:authorize="hasAuthority('job:add')" class="layui-btn layui-btn-xs" lay-event="add"><i
                            class="layui-icon layui-icon-add-1"></i>添加</a>
                </div>
            </script>
            <script type="text/html" id="enabledTpl">
                <input type="checkbox" lay-filter="enabled" value="{{d.id}}" lay-skin="switch" lay-text="启用|禁用"
                       {{d.enabled==='Y'?'checked':''}}/>
            </script>
            <script type="text/html" id="tableBar">
                <a sec:authorize="hasAuthority('job:log')" class="layui-btn layui-btn-normal layui-btn-xs"
                   lay-event="log"><i class="layui-icon"></i>调度日志</a>
                <a  class="layui-btn layui-btn-normal layui-btn-xs"
                   lay-event="runnow"><i class="layui-icon"></i>立即执行</a>
                <a sec:authorize="hasAuthority('job:edit')" class="layui-btn layui-btn-normal layui-btn-xs"
                   lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
                <a sec:authorize="hasAuthority('job:del')" class="layui-btn layui-btn-danger layui-btn-xs"
                   lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>
            </script>
        </div>
    </div>
</div>
<div th:replace="common/script::footer"></div>
<script th:inline="javascript">
    layui.config({
        base: '/static/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'useradmin', 'table', 'crud'], function () {
        let $ = layui.$,
            form = layui.form,
            table = layui.table,
            crud = layui.crud;
        //监听搜索
        form.on('submit(search)', function (data) {
            let field = data.field;
            //执行重载
            table.reload('jobTable', {
                where: field
            });
        });
        // 修改任务状态
        form.on('switch(enabled)', function (obj) {
            let id = obj.elem.value;
            let checked = obj.elem.checked ? 'Y' : 'N';
            active.changeJobEnabled(id, checked);
        });
        //监听重置
        form.on('submit(reset)', function (data) {
            Object.keys(data.field).forEach(key => (data.field[key] = ''));
            table.reload('jobTable', {
                where: data.field
            });
        });
        // 查询列表接口
        table.render({
            elem: '#jobTable'
            , toolbar: '#toolbar'
            , height: 'full-110'
            , url: ctx + '/qrtz/job/page'
            , cellMinWidth: 80
            , page: true //开启分页
            , cols: [[ //表头
                {type: 'checkbox', fixed: 'left'},
                {field: 'id', title: 'ID', hide: true},
                {field: 'jobName', title: '任务名字'},
                {field: 'jobGroup', title: '任务组'},
                {field: 'cron', title: 'cron表达式'},
                {field: 'jobClassName', title: '类名称'},
                {field: 'enabled', align: "center", templet: '#enabledTpl', title: '任务状态'},
                {toolbar: '#tableBar', title: '操作', width: 300, align: 'center', fixed: 'right'}
            ]]
        });
        //头工具栏事件
        table.on('toolbar(jobTable)', function (obj) {
            let checkStatus = table.checkStatus(obj.config.id)
                , data = checkStatus.data;
            switch (obj.event) {
                case 'add':
                    active.addView();
                    break;
            }
        });
        //监听行工具事件
        table.on('tool(jobTable)', function (obj) {
            let data = obj.data //获得当前行数据
                , layEvent = obj.event;
            if (layEvent === 'edit') {
                active.editView(data.id);
            } else if (layEvent === 'del') {
                active.del(data.id);
            } else if (layEvent === 'log') {
                active.log(data.id);
            } else if (layEvent === 'runnow') {
                active.runnow(data.id);
            }
        });
        /* 触发弹层 */
        let active = {
            /**
             * 修改任务状态
             * @param id 任务id
             * @param checked 状态
             */
            changeJobEnabled: function (id, checked) {
                $.ajax({
                    type: 'POST',
                    url: ctx + '/qrtz/job/changeJob',
                    data: JSON.stringify({
                        id: id,
                        enabled: checked
                    }),
                    contentType: 'application/json;charset=UTF-8',
                    dataType: 'json',
                    success: function (result) {
                        layer.msg(result.message);
                        if (result.code === 200) {
                            table.reload('jobTable');
                        }
                    }
                });
            },
            /**
             * 添加
             * */
            addView: function () {
                layer.open({
                    type: 2
                    , shade: 0.3
                    , title: "添加任务表"
                    , content: ctx + '/qrtz/job/addPage'
                    , maxmin: true
                    , scrollbar: false
                    , area: ['100%', '100%']
                    , btn: ['确定', '取消']
                    , yes: function (index, layero) {
                        let submit = layero.find('iframe').contents().find('#save-submit');
                        submit.trigger('click');
                    }
                });
            },
            /**
             * 编辑
             * @param id id
             * */
            editView: function (id) {
                layer.open({
                    type: 2
                    , shade: 0.3
                    , title: '修改任务表'
                    , content: ctx + '/qrtz/job/editPage/' + id
                    , maxmin: true
                    , scrollbar: false
                    , area: ['100%', '100%']
                    , btn: ['确定', '取消']
                    , yes: function (index, layero) {
                        let submit = layero.find('iframe').contents().find('#save-submit');
                        submit.trigger('click');
                    }
                });
            },
            /**
             * 删除
             * @param id id
             */
            del: function (id) {
                layer.confirm('真的删除行么', function (index) {
                    //向服务端发送删除指令
                    $.ajax({
                        type: 'DELETE',
                        url: ctx + '/qrtz/job/remove/' + id,
                        contentType: 'application/json;charset=UTF-8',
                        dataType: 'json',
                        success: function (result) {
                            layer.msg(result.message);
                            if (result.code === 200) {
                                layer.close(index);
                                table.reload('jobTable');
                            }
                        }
                    });
                });
            },
            /**
             * 调度日志
             * @param jobId 主键
             */
            log: function (jobId) {
                let url = ctx + "/qrtz/jobLog/listPage?jobId=" + jobId;
                let title = "调度日志";
                top.layui.index.openTabsPage(url, title)
            },

            /**
             * 立即执行
             * @param jobId 主键
             */
            runnow: function (jobId) {
                let url = ctx + "/qrtz/job/run?jobId=" + jobId;
                let title = "立即执行";
                top.layui.index.openTabsPage(url, title)
            }
        }
    });
</script>
</body>
</html>
